package com.example.demo.bean;

import com.example.demo.org.EntityIdentRelation;
import com.example.demo.org.EntityObjectRelation;
import com.example.demo.org.ObjectRelationListResult;
import com.example.demo.org.ObjectRelationUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.concurrent.CompletableFuture;

@Component
public class EntityPicService {
    @Value("${cw.org.web.service.obj.url}")
    private String objUrl;

    public EntityObjectRelation getEntityObjectRelation(String entityUuid) throws Exception {
        EntityObjectRelation entityObjectRelation = new EntityObjectRelation();
        entityObjectRelation.setEntityUuid(entityUuid);

        String logoUrl = objUrl + "/entities/" + entityUuid + "/relations/" + entityUuid + "/objects/listall?relatonTag=relaton_tag_entity_logo";
        ObjectRelationListResult logoResult = ObjectRelationUtil.getobjectRelationList(logoUrl);
        if(!CollectionUtils.isEmpty(logoResult.getContent())){
            String picUrl = logoResult.getContent().get(0).getObjUrl();
            if(!StringUtils.isEmpty(picUrl)){
                picUrl = picUrl.substring(0,picUrl.indexOf("?"));
            }
            entityObjectRelation.setEntityLogoUuid(logoResult.getContent().get(0).getUuid());
            entityObjectRelation.setEntityLogoUrl(picUrl);
        }

        String headPicUrl = objUrl + "/entities/" + entityUuid + "/relations/" + entityUuid + "/objects/listall?relatonTag=relaton_tag_entity_head_pic";
        ObjectRelationListResult headPicResult = ObjectRelationUtil.getobjectRelationList(headPicUrl);
        if(!CollectionUtils.isEmpty(headPicResult.getContent())){
            String picUrl = headPicResult.getContent().get(0).getObjUrl();
            if(!StringUtils.isEmpty(picUrl)){
                picUrl = picUrl.substring(0,picUrl.indexOf("?"));
            }
            entityObjectRelation.setEntityHeadPicUuid(headPicResult.getContent().get(0).getUuid());
            entityObjectRelation.setEntityHeadPicUrl(picUrl);
        }

        return entityObjectRelation;
    }

    @Async("taskExecutor")
    public CompletableFuture<EntityObjectRelation> getEntityObjectRelationAsync(String entityUuid) {
        CompletableFuture<EntityObjectRelation> future = new CompletableFuture<>();
        try {
            EntityObjectRelation entityObjectRelation = getEntityObjectRelation(entityUuid);
            future.complete(entityObjectRelation);
        } catch (Exception e) {
            future.complete(null);
        }
        return future;
    }

    public EntityIdentRelation getEntityIdentRelation(String entityUuid) throws Exception {
        EntityIdentRelation entityIdentRelation = new EntityIdentRelation();
//        entityIdentRelation.setEntityUuid(entityUuid);

        String licensePicUrl = objUrl + "/entities/" + entityUuid + "/relations/" + entityUuid + "/objects/listall?relatonTag=relaton_tag_entity_ident_license_pic";
        ObjectRelationListResult licensePicResult = ObjectRelationUtil.getobjectRelationList(licensePicUrl);
        if(!CollectionUtils.isEmpty(licensePicResult.getContent())){
            entityIdentRelation.setLicensePicUuid(licensePicResult.getContent().get(0).getUuid());
            String picUrl = licensePicResult.getContent().get(0).getObjUrl();
            if(!StringUtils.isEmpty(picUrl)){
                picUrl = picUrl.substring(0,picUrl.indexOf("?"));
            }
            entityIdentRelation.setLicensePicUrl(picUrl);
        }

        String orgPicUrl = objUrl + "/entities/" + entityUuid + "/relations/" + entityUuid + "/objects/listall?relatonTag=relaton_tag_entity_ident_org_pic";
        ObjectRelationListResult orgPicResult = ObjectRelationUtil.getobjectRelationList(orgPicUrl);
        if(!CollectionUtils.isEmpty(orgPicResult.getContent())){
            entityIdentRelation.setOrgPicUuid(orgPicResult.getContent().get(0).getUuid());
            String picUrl = orgPicResult.getContent().get(0).getObjUrl();
            if(!StringUtils.isEmpty(picUrl)){
                picUrl = picUrl.substring(0,picUrl.indexOf("?"));
            }
            entityIdentRelation.setOrgPicUrl(picUrl);
        }

        return entityIdentRelation;
    }


}